package com.mzjf.boot.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.mzjf.boot.common.PageVo;
import com.mzjf.boot.common.RedisDao;
import com.mzjf.boot.config.RedisConfiguration;
import com.mzjf.boot.dao.SysuserMapper;
import com.mzjf.boot.entity.Sysuser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    private static final Log    log   = LogFactory.getLog(RedisConfiguration.class);
    public final static     String READ_BOOK_BY_SYSUSER = "sysuser:";
    @Autowired
    RedisDao redisDao;
    @Autowired
    private SysuserMapper sysuserMapper; //用户接口

    /**
     * @return
     * @throws Exception
     * @Title: getList
     * @Description: 从数据库中获取所有商品类型列表
     */
    public PageVo<Sysuser> selectUser() {
        int pageNum = 1;
        int pageSize = 50;
        PageVo<Sysuser> pageVo = new PageVo();
        pageVo.setPageSize(pageSize);
        pageVo.setPage(pageNum);//当前页
        PageHelper.startPage(pageNum, pageSize);
        Sysuser user = new Sysuser();
        user.setStatus(1);
        List list = sysuserMapper.selectUserList(user);
        pageVo.setData(list);
        return pageVo;
    }
    //@param pageNum  当前页
    // * @param pageSize 当前页面展示数目
    /**
     * @return User
     * @throws Exception
     * @Title: getUser
     * @Description: 更具id查询 用户列表
     */

    public PageVo<Sysuser> selectUserById(Integer Id) {
        int pageNum = 1;
        int pageSize = 50;
        PageVo<Sysuser> pageVo = new PageVo();
        pageVo.setPageSize(pageSize);
        pageVo.setPage(pageNum);//当前页
        PageHelper.startPage(pageNum, pageSize);
        Sysuser user = new Sysuser();
        List<Sysuser> list = sysuserMapper.selectByPrimaryKey(Id);
        log.info(list);
        //查詢緩存
        String sysUserString=(String)redisDao.get(READ_BOOK_BY_SYSUSER+list.get(0).getId());
        Sysuser sysuser=(Sysuser) JSON.parseObject(sysUserString,Sysuser.class);
        log.info(sysuser);
        //添加緩存
        String listJson = JSON.toJSONString(list.get(0));
        redisDao.set(READ_BOOK_BY_SYSUSER+list.get(0).getId(), listJson);
        pageVo.setData(list);

        return pageVo;
    }
}
